package com.atguigu.paymentdemo.service.impl;

import com.atguigu.paymentdemo.entity.OrderInfo;
import com.atguigu.paymentdemo.entity.RefundInfo;
import com.atguigu.paymentdemo.enums.wxpay.WxRefundStatus;
import com.atguigu.paymentdemo.mapper.RefundInfoMapper;
import com.atguigu.paymentdemo.service.OrderInfoService;
import com.atguigu.paymentdemo.service.RefundInfoService;
import com.atguigu.paymentdemo.util.OrderNoUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Time;
import java.time.Duration;
import java.time.Instant;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class RefundInfoServiceImpl extends ServiceImpl<RefundInfoMapper, RefundInfo> implements RefundInfoService {

    @Autowired
    private OrderInfoService orderInfoService;

    /**
     * @Description: 创建退款单
     * @param orderNo   订单号
     * @param reason    退款原因
     * @return
     */
    @Override
    public RefundInfo createRefundByOrderNo(String orderNo, String reason) {
        //获取订单信息
        OrderInfo orderInfo = orderInfoService.getOrderByOrderNo(orderNo);

        //生成退单信息
        RefundInfo refundInfo = new RefundInfo();
        refundInfo.setOrderNo(orderNo);
        refundInfo.setRefundNo(OrderNoUtils.getRefundNo());
        refundInfo.setReason(reason);
        refundInfo.setTotalFee(orderInfo.getTotalFee());
        refundInfo.setRefund(orderInfo.getTotalFee());

        baseMapper.insert(refundInfo);
        return refundInfo;
    }


    /**
     * @Description: 更新退款单的状态
     * @param orderNo   订单编号
     * @param bodyAsString  wx报文
     */
    @Override
    public void updateRefund(String bodyAsString) {
        Gson gson = new Gson();
        HashMap hashMap = gson.fromJson(bodyAsString, HashMap.class);

        String outTradeNo = (String) hashMap.get("out_trade_no");
        LambdaQueryWrapper<RefundInfo> refundInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        refundInfoLambdaQueryWrapper.eq(RefundInfo::getOrderNo,outTradeNo);

        RefundInfo refundInfo = new RefundInfo();
        refundInfo.setOrderNo(outTradeNo);

        String refundId = (String) hashMap.get("refund_id");
        refundInfo.setRefundId(refundId);

        if(hashMap.get("status")!=null){
            refundInfo.setRefundStatus((String) hashMap.get("status"));
            refundInfo.setContentReturn(bodyAsString);
        }

        if(hashMap.get("refund_status")!=null){
            refundInfo.setRefundStatus((String) hashMap.get("refund_status"));
            refundInfo.setContentNotify(bodyAsString);
        }

        baseMapper.update(refundInfo,refundInfoLambdaQueryWrapper);
    }

    /**
     * @Description: 查询5分钟以前的退款中的退款单
     * @param minute    分钟
     * @return
     */
    @Override
    public List<RefundInfo> getProcessingRedundInfoByDuration(int minute) {
        Instant time = Instant.now().minus(Duration.ofMinutes(minute));

        LambdaQueryWrapper<RefundInfo> refundInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        refundInfoLambdaQueryWrapper.eq(RefundInfo::getRefundStatus, WxRefundStatus.PROCESSING.getType());
        refundInfoLambdaQueryWrapper.lt(RefundInfo::getCreateTime, time);

        return baseMapper.selectList(refundInfoLambdaQueryWrapper);
    }
}
